//
//  BRMInfoContactView.m
//  BrezzeSaaS
//
//  Created by macos on 16.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMInfoContactView.h"


@implementation BRMInfoContactView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)toMapClick {
    
    if (self.toMap) {
        self.toMap();
    }
}

- (void)toSelectIndustry {
    if (self.toIndustryBlcok) {
        self.toIndustryBlcok();
    }
}


- (void)setTitle:(NSString *)title by:(NSInteger)index {
    
    self.isPutIn = YES;
//    if (index == 3) {
//        BRMInfoListMoreView *cell = [self viewWithTag:index + 100];
//        cell.titleStr = title;
//        return;
//    }
    BRMInfoListView *cell = (BRMInfoListView *)[self viewWithTag:index + 100];
    cell.textView.text = title;
    [cell.textView layoutIfNeeded];
    
}

- (void)br_setOtp:(NSInteger)code by:(NSInteger)index {
    
    BRMInfoListMoreView *cell = [self viewWithTag:index + 100];
    cell.otpView.codeL.text = [NSString stringWithFormat:@"%ld",code];
}

#pragma mark - UI --
- (void)setUI {
    self.layer.cornerRadius = 13;
    self.layer.masksToBounds = YES;
    self.backgroundColor = UIColorWhite;
    
    NSArray *titles = @[Localized(@"shop_merchantName"),
                        Localized(@"shop_industryCategory"),
                        Localized(@"shop_location"),
                        Localized(@"shop_address"),
                        Localized(@"shop_franchisee"),
                        Localized(@"shop_profitAllocation"),
                        Localized(@"shop_pricePlan")];
    NSString *placeHolders = Localized(@"shop_pleaseEnter");
    
    UIView *firstView;
    for (int i = 0; i < titles.count; i++) {
        NSString *title = titles[i];

        if ([title isEqualToString:Localized(@"shop_contactNum")]) {
            BRMInfoListMoreView *cell = [[BRMInfoListMoreView alloc] init];
//            cell.placeholder = Localized(@"public_pleaseEnter");
            cell.textView.userInteractionEnabled = [self canEdit:i];
            cell.titleStr = title;
            cell.placeholder = placeHolders;
            [cell br_showOtpView];
            cell.listType = BRMShopInf_phone;
            [self addSubview:cell];
            [cell mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                if (i == 0) {
                    make.top.offset(46);
                }else {
                    make.top.equalTo(firstView.mas_bottom).offset(0);
                }
                if (i == titles.count - 1) {
                    make.bottom.offset(0);
                }
            }];
            cell.tag = 100 + i;
            firstView = cell;
            WS(weakSelf);
            cell.textChangBlock = ^(NSString * _Nonnull text) {
                if (weakSelf.contactTextChangBlock && !weakSelf.isPutIn) {
                    weakSelf.contactTextChangBlock(@{@"title":title,@"text":text,@"index":@(i)});
                }
                weakSelf.isPutIn = NO;
            };
            cell.toOtpBlock = ^{
                if (weakSelf.toOtpBlock) weakSelf.toOtpBlock();
            };
            continue;
        }
            
        
        BRMInfoListView *cell = [[BRMInfoListView alloc] init];
        cell.textView.editable = i < 5;
        cell.titleStr = title;
        cell.placeholder = placeHolders;
        [self addSubview:cell];
        [cell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            if (i == 0) {
                make.top.offset(46);
            }else {
                make.top.equalTo(firstView.mas_bottom).offset(0);
            }
            if (i == titles.count - 1) {
                make.bottom.offset(0);
            }
        }];
        if (i == 0) cell.listType = BRMShopInf_name;
        if (i == 4) cell.listType = BRMShopInf_phone;
        cell.moreImgHidde = i != 2;
        
        cell.textView.userInteractionEnabled = [self canEdit:i];
        
        cell.tag = 100 + i;
        firstView = cell;
        if (i == titles.count - 1) {
            cell.lineHidde = YES;
        }
        if ([title isEqualToString:Localized(@"shop_industryCategory")]) {
            UIImageView *rimg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_triangle_Black8")];
            rimg.tag = 300 + i;
            [cell addSubview:rimg];
            [rimg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.textView.mas_centerY);
                make.right.offset(-16);
                make.width.mas_equalTo(8);
                make.height.mas_equalTo(5);
            }];
            cell.textView.userInteractionEnabled = NO;
            UIControl *industryBtn = [[UIControl alloc] init];
            industryBtn.tag = 200 + i;
            [cell addSubview:industryBtn];
            [industryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(cell);
            }];
       
            [industryBtn addTarget:self action:@selector(toSelectIndustry) forControlEvents:UIControlEventTouchUpInside];
        }
        if ([title isEqualToString:Localized(@"shop_location")]) {
            
            UIImageView *rimg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_right_grayLow6")];
            [cell addSubview:rimg];
            [rimg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.titleL.mas_centerY);
                make.right.offset(-16);
                make.width.mas_equalTo(6);
                make.height.mas_equalTo(10);
            }];
            cell.leftImgStr = @"merchant_location";
            cell.textView.userInteractionEnabled = NO;
            UIControl *mapBtn = [[UIControl alloc] init];
            [cell addSubview:mapBtn];
            [mapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(cell);
            }];
            [mapBtn addTarget:self action:@selector(toMapClick) forControlEvents:UIControlEventTouchUpInside];
        }
        if ([title isEqualToString:Localized(@"shop_address")]) {
            cell.textView.maximumTextLength = 250;
            
        }
        WS(weakSelf);
        cell.textChangBlock = ^(NSString * _Nonnull text) {
            if (weakSelf.contactTextChangBlock && !weakSelf.isPutIn) {
                weakSelf.contactTextChangBlock(@{@"title":title,@"text":text,@"index":@(i)});
            }
            weakSelf.isPutIn = NO;
        };
        
        
    }
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"shop_merchantInfo") textColor:UIColor.qd_titleTextColor font:[UIFont fontS:17]];
    [self addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(16);
        make.height.mas_greaterThanOrEqualTo(20);
    }];
}

- (BOOL)canEdit:(NSInteger)index {
    NSArray *canEdits = @[@"0",@"1",@"2",@"3"];
    
    return [canEdits containsObject:[NSString stringWithFormat:@"%ld",index]];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
